package com.fairytask.ui.mainUnit
{
	import com.fairytask.data.ObjectData;
	import com.fairytask.engine.components.checkBox.CheckBox;
	import com.fairytask.engine.components.clip.Clip;
	import com.fairytask.engine.components.text.Label;
	import com.fairytask.resource.StaticIni;
	import com.fairytask.tools.TipsTools;
	import com.fairytask.ui.InfoUI;
	import com.fairytask.ui.base.UnitUI;
	import com.fairytask.ui.equipUnit.EquipPart;
	import com.fairytask.ui.equipUnit.PropertyPart;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	/**
	 * 装备栏属性UI 
	 * @author xujh
	 * 
	 */
	public class ProfileUI extends UnitUI
	{
		private static var _self		:ProfileUI;
		//=========显示面板====================================================
		//属性数值
//		public var equip_value:Label;	//装备评分
//		public var equip_value_input:Input;//装备评分
		public var propSp		:Sprite = new Sprite();
		public var expLabel	:Clip;	//经验
		public var expInput	:Label;
		public var expBar		:MovieClip;	//经验条
		
		//=============宠物部分显示=====================
		public var petSpace	:Sprite = new Sprite();
		public var petNameLab	:Label;
		public var petLevelLab	:Label;
		public var petSprLab	:Label;		//所需灵
		//宠物评分星级容器
		private var starSpace	:Sprite = new Sprite();
		
		private var _info		:InfoUI		=	InfoUI.getInstance();
		
		//===================记录的数据========================
		private var _isPlayer	:Boolean;	//是人物
		private var _curObj	:Point;		//当前对象
		private var _data			:ObjectData;	//显示对象
		private var _bindArr		:Array	=[];	//绑定数据的数组，用于解绑
		private var _initBind		:Boolean = false;	//第一次打开时做的绑定
		private var _lvExp		:int;
		private var _nowExp	:int;
		
		private var _tmpPos	:int;
		private var _nameStr	:String;
		private var _color		:int;
		private var _curView	:int;		//当前装备视图
		private var _equipPart	:EquipPart		= new EquipPart();
		private var _propPart	:PropertyPart	= new PropertyPart();
		
		private const MAXSTAR	:int = 5;
		
		public static function getInstance():ProfileUI
		{
			if (_self == null)
			{
				_self=new ProfileUI;
			}
			return _self;
		}
		
		public function ProfileUI()
		{
			super();
		}
		
		override public function initGUI():void
		{
			
			//初始化宠物人物不同信息显示容器
			propSp.x = 5;
			propSp.y = 75;
			windowBG.addChild(propSp);
			petSpace.x = 10;
			petSpace.y = 85;
			windowBG.addChild(petSpace);
			starSpace.x = 100;
			starSpace.y = 175;
			petSpace.addChild(starSpace);
			/*starSpace.x = 20;
			starSpace.y = 200;
			petSpace.addChild(starSpace);*/
			
			//初始化星星
			initStar();
			
			_equipPart.canClick = true;
			windowBG.addChild(_equipPart);
			propSp.addChild(_propPart);
		}
		
		private function initStar():void
		{
			for(var i:int=0;i<MAXSTAR;i++)
			{
				var star	:CheckBox = new CheckBox("checkbox_002");
				star.selected = false;
				star.isEvent = false;
				star.width = star.height = 17;
				star.x = i*17;
				starSpace.addChild(star);
			}
		}
		
		/**
		 * 添加数据绑定 
		 * 
		 */
		private function addBindData():void
		{
			_bindArr.push([_data,"Name",bindManager.BindProp(_data, "Name",this,"rName")]);	//名字
			_bindArr.push([_data,"Level",bindManager.BindProp(_data, "Level",this,"rlevel")]);			//等级
			_bindArr.push([_data,"PlayerExp",bindManager.BindProp(_data, "PlayerExp",this,"rExp")]);	//经验
			
			_bindArr.push([_data,"Model",bindManager.BindProp(_data, "Model",_equipPart,"roleModel")]);		//人物模型
			_bindArr.push([_data,"WeaponModel",bindManager.BindProp(_data, "WeaponModel",_equipPart,"weaponModel")]);//武器模型
			_bindArr.push([_data,"WeaponModel",bindManager.BindProp(_data, "WeaponLevel",_equipPart,"WeaponLevel")]);//武器特效
			if(!_isPlayer)
			{
				_bindArr.push([_data,"Color",bindManager.BindProp(_data, "Color",this,"rColor")]);	//获取颜色
				starSpace.visible = true;
			}
			else
			{
				starSpace.visible = false;
			}
			//绑定基本属性
			_propPart.bindProp(_data);
		}
		
		/**
		 * 绑定视图 
		 * 
		 */
		private function setBindView():void
		{
			_equipPart.bindView(_curView);
		}
		
		/**
		 * 解绑数据 
		 * 
		 */
		private function removeBindData():void
		{
			//全部解绑
			for each(var itemBind:Array in _bindArr)
			{
				bindManager.UnbindProp(itemBind.shift(),itemBind.shift(),itemBind.shift());
			}
			_bindArr = [];
		}
		
		
		//==========================绑定数据处理=======================================
		//设置颜色
		public function set rColor(val:int):void
		{
			_color = val;
			petNameLab.htmlStrByBorder = TipsTools.link_color(_nameStr,_color,16);
			petLevelLab.x = petNameLab.x + petNameLab.width + 5;
			starSpace.x = petLevelLab.x + petLevelLab.width + 15;
			addStar(val-1);
		}
		
		/**
		 * 添加星星 
		 * @param val
		 * 
		 */
		private function addStar(val:int):void
		{
			for(var i:int=0;i<val;i++)
			{
				if(i<starSpace.numChildren)
				{
					(starSpace.getChildAt(i) as CheckBox).selected = true;
				}
			}
		}
		
		//设置名字
		public function set rName(val:String):void
		{
			petNameLab.htmlStrByBorder = TipsTools.link_color(val,_color,16);
			_nameStr = val;
			petLevelLab.x = petNameLab.x + petNameLab.width + 5;
		}
		
		/**
		 * 当前经验 
		 * @param val
		 * 
		 */		
		public function set rExp(val:int):void
		{;
			//当前经验
			val > _lvExp ? _nowExp = _lvExp : _nowExp = val;
			expInput.htmlStr = _nowExp + "/" + _lvExp;
			expInput.x = expBar.x + (expBar.width - expInput.width) / 2;
			_lvExp != 0 ? expBar["maskBar"].width = expBar.width * _nowExp / _lvExp : expBar["maskBar"].width = 0;
		}
		
		/**
		 * 设置等级 
		 * @param val
		 * 
		 */
		public function set rlevel(val:int):void
		{
			petLevelLab.htmlStrByBorder = StaticIni.getSwfLangStr("1000000062") + val;
			starSpace.x = petLevelLab.x + petLevelLab.width + 15;
			_lvExp = int(StaticIni.getIniVar("lv_" + val,"EXP"));
			expInput.htmlStr = _nowExp + "/" + _lvExp;
			expInput.x = expBar.x + (expBar.width - expInput.width) / 2;
			_lvExp != 0 ? expBar["maskBar"].width = expBar.width * _nowExp / _lvExp : expBar["maskBar"].width = 0;
		}
		//==========================绑定数据处理完毕=======================================
		
		/**
		 * 设置属性数据
		 * 
		 */
		public function set data(val:*):void
		{
			//数据相同返回不做操作
			if(_data == val)
			{
				return;
			}
			//先清除数据,第一次不清除
			if(_initBind)
			{
				clearAll();
			}
			_data = val;
			//设置当前数据是否是人物
			if(null != _data)
			{
				//是人物
				/*if(_data.ObjKey == Player.instance.data.ObjKey)
				{
					_curObj = (_data as SceneObject).ObjID;
					_curView = ViewDefine.VIEWPORT_EQUIP;
					isNeedFresh(true);
				}
				//宠物
				else
				{
					_curObj = (_data as ViewObject).objId;
					//判断是不是宠物装备视图已经存在，不存在请求视图
					_curView = 200 + (val as ViewObject).GetPropertiesEx("Pos");
					if(!model.scene.CheckView(_curView))
					{
						//NetMgr.sendCustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_CHANGE_EQUIP_OBJ,_curObj);
					}
					isNeedFresh(false);
				}*/
			}
			
		}
		
		/**
		 * 和原先的状态相同就不需要解绑重新绑定
		 * 
		 */
		private function isNeedFresh(val:Boolean):void
		{
			if(_initBind)
			{
				removeBindData();
			}
			else
			{
				_initBind = true;
			}
			//是否重新绑定视图
			/*if(_isPlayer != val)
			{
				_isPlayer = val;
				setBindView();
			}*/
			_isPlayer = val;
			//重新对数据进行绑定
			addBindData();
			setBindView();
		}
		
		/**
		 * 当前视图id  
		 * @return 
		 * 
		 */
		public function get equipViewId():int
		{
			return _equipPart.curViewId;
		}
		//==========================清除方法=======================================
		/**
		 * 清除所有内容，为加载新的内容做准备 
		 * 
		 */
		public function clearAll():void
		{
			//隐藏宠物星
			for(var i:int=0;i<starSpace.numChildren;i++)
			{
				(starSpace.getChildAt(i) as CheckBox).selected = false;
			}
			//清除内容
			_lvExp	= 0;
			rExp	= 0;
			expInput.htmlStr = "";
			petNameLab.htmlStrByBorder = "";
			petLevelLab.htmlStrByBorder = "";
			_data = null;
			_color = 0;
			RefineUI.getInstance().clear();
			RecastUI.getInstance().clear();
			UniverseUI.getInstance().UIShow();
		}
		
		/**
		 * 重置，默认打开人装备 
		 * 
		 */
		override public function reset():void
		{
			//this.data = Player.instance.data;
		}
	}
}